/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.server.inGame;

import core.messages.client.inGame.Msg_Response;
import core.messages.client.inGame.Msg_Response_Yes;
import core.messages.server.inGame.Msg_Response_Accept;
import core.threads.server.Server_Public_Data;
import core.threads.server.Server_Public_MsgBox;
import cstAndEnum.JUDGE_TYPE;
import models.Card.CardSpell;
import models.CastInfo;
import models.Game;
import models.Player;

/**
 *
 * @author Invince
 */
public class T_Server_Card_Judger  extends Thread{
/*----------Attributs-------------------*/
//    protected boolean stop;
//    protected boolean suspend;

    /**
      * 回应一张牌时，最后一个回应者
      */
    protected int lastResponserIndex;
    /**
      * 回应一张牌时，最后一个有回应者
      */
    protected int lastResponserYesIndex;
    /**
       * 回应一张牌时，最后一张回应牌
       */
   protected CastInfo lastCastInfo;
   /**
    * 判定类型
    */
   protected JUDGE_TYPE judgeType;
   /**
    * 判定过与否,初始为false
    */
   protected boolean pass;


/*----------Public functions------------*/
    @Override
    public void run() {
        int nextResponserIndex=(this.lastResponserIndex=getNextResponser());
        Game game;
        synchronized(Server_Public_Data.semaGame){
            game=Server_Public_Data.game;
        }
        Player nextResponser=game.getListPlayer().get(nextResponserIndex);
        Msg_Response_Accept msgRspA=new Msg_Response_Accept("Server", nextResponser.getName(),this.lastCastInfo);
        Server_Public_MsgBox.msgBox_Server_Send_Msg.offer(msgRspA);
        boolean doIt=true;
        while(lastResponserYesIndex!=lastResponserIndex){//第一次进到boucle时，lastResponserIndex已经经过一次getNextResponser了
            if(!Server_Public_MsgBox.msgBox_Server_Response.isEmpty()){
                Msg_Response msgRsp=Server_Public_MsgBox.msgBox_Server_Response.poll();
                if(msgRsp instanceof Msg_Response_Yes){
                    Msg_Response_Yes msgRY=(Msg_Response_Yes) msgRsp;
                    //处理response
                    doIt&=handleResult(msgRY);
                    //处理牌库（其实发送一个处理牌库的消息）
                    Server_Public_MsgBox.Deck_Process_Move dPm=new Server_Public_MsgBox.Deck_Process_Move(-1, 2, msgRY.getIdCard());//-1表示Server,2为弃牌堆
                    Server_Public_MsgBox.msgBox_Deck_Process.offer(dPm);
                    //处理本地信息
                    Player lastResponser=game.getListPlayer().findPlayerByName(msgRY.getSender());
                    this.lastResponserYesIndex=game.getListPlayer().getList().indexOf(lastResponser);
                    this.lastCastInfo=msgRY.getCastInfo();
                }
                //发送下一个可以回应的消息
                nextResponserIndex=(this.lastResponserIndex=getNextResponser());
                nextResponser=game.getListPlayer().get(nextResponserIndex);
                msgRspA=new Msg_Response_Accept("Server", nextResponser.getName(),this.lastCastInfo);
                Server_Public_MsgBox.msgBox_Server_Send_Msg.offer(msgRspA);
                //更新game，因为数据处理都是直接在Server_Public_Data里做的，而这里的game并没有更新
                synchronized(Server_Public_Data.semaGame){
                    game=Server_Public_Data.game;
                }
                //刷新屏幕                
                Server_Public_MsgBox.refreshScreen();
            }
        }
        Server_Public_MsgBox.refreshScreen();//这个也许不需要，但是为防没进boucle

        if(doIt){
            drawJudgeCard();
        }
    }

//    public void stopT(){
//        synchronized(this){
//            this.stop=true;
//        }
//    }
//
//    public void suspendT(){
//        synchronized(this){
//            this.suspend=true;
//        }
//    }
//
//    public void resumeT(){
//        synchronized(this){
//            this.suspend=false;
//        }
//    }

    /**
     * 获取下一个回应者的index
     * @return
     */
    public static int getNextResponser(){
        if(T_Server_Turn_Controller.getLastPlayerIndex()==-1){//Means the very beginning of the game
            return Server_Public_Data.game.getMajestyID();
        }else{
            int v=T_Server_Turn_Controller.getLastPlayerIndex();
            T_Server_Turn_Controller.setLastPlayerIndex(v+1);
            int index=T_Server_Turn_Controller.getLastPlayerIndex();
            if(index>=Server_Public_Data.game.getListPlayer().size()){
                index=0;
            }
            return index;
        }
    }


    public boolean handleResult(Msg_Response_Yes msgRY){
        CastInfo castInfo=msgRY.getCastInfo();
        if(lastCastInfo.getCard() instanceof CardSpell){
            if(castInfo.getCard().getName().equals("无懈可击")){
                return false;
            }
        }
        return true;
    }

    /**
     * 判定抽牌
     */
    public Card drawJudgeCard(){//??????????????换判定牌
        Game game;
        synchronized(Server_Public_Data.semaGame){
            game=Server_Public_Data.game;
        }

    }
/*----------Private functions-----------*/
/*----------Constructor-----------------*/
    public T_Server_Card_Judger(int judgeIndex,CastInfo lastCastInfo,JUDGE_TYPE judgeType) {
//        synchronized(this){
//            this.stop=false;
//            this.suspend=false;
//        }
        this.lastResponserIndex=judgeIndex;
        this.lastResponserYesIndex=judgeIndex;
        this.lastCastInfo=lastCastInfo;
        this.judgeType=judgeType;
        this.pass=false;
    }
    
/*----------Gets and sets---------------*/
    public JUDGE_TYPE getJudgeType() {
        return judgeType;
    }

    public void setJudgeType(JUDGE_TYPE judgeType) {
        this.judgeType = judgeType;
    }

    public CastInfo getLastCastInfo() {
        return lastCastInfo;
    }

    public void setLastCastInfo(CastInfo lastCastInfo) {
        this.lastCastInfo = lastCastInfo;
    }

    public int getLastResponserIndex() {
        return lastResponserIndex;
    }

    public void setLastResponserIndex(int lastResponserIndex) {
        this.lastResponserIndex = lastResponserIndex;
    }

    public int getLastResponserYesIndex() {
        return lastResponserYesIndex;
    }

    public void setLastResponserYesIndex(int lastResponserYesIndex) {
        this.lastResponserYesIndex = lastResponserYesIndex;
    }
  
}
